package view.panels;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
 *
 * @author
 * Guerino
 */
public class SearchJPanel extends javax.swing.JPanel {
    private Timer animator = null;
    private int controlsHeight = 0;
    private int controlsPreferredHeight = 0; 
    private boolean isHidden = true;
    
    
    private final JPanel controls = new JPanel(new BorderLayout(5, 5) {
        private int controlsHeight = 0;
        private int controlsPreferredHeight = 0;
            @Override public Dimension preferredLayoutSize(Container target) {
                //synchronized (target.getTreeLock()) {
                Dimension ps = super.preferredLayoutSize(target);
                controlsPreferredHeight = ps.height;
                if(animator!=null) {
                    if(isHidden) {
                        if(controls.getHeight()<controlsPreferredHeight) controlsHeight += 5;
                    }else{
                        if(controls.getHeight()>0) controlsHeight -= 5;
                    }
                    if(controlsHeight<=0) {
                        controlsHeight = 0;
                        animator.stop();
                    }else if(controlsHeight>=controlsPreferredHeight) {
                        controlsHeight = controlsPreferredHeight;
                        animator.stop();
                    }
                }
                ps.height = controlsHeight;
                return ps;
            }
    });
    
    /**
     * Creates
     * new
     * form
     * SearchJPanel
     */
    public SearchJPanel() {
        initComponents();       
        
        //controls.add(jPanelResultadoBusqueda);
    }      
    
   

     
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextFieldSearch = new javax.swing.JTextField();
        jButtonSearch = new javax.swing.JButton();
        jPanelResultadoBusqueda = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListResultados = new javax.swing.JList();

        jButtonSearch.setText("Buscar");
        jButtonSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSearchActionPerformed(evt);
            }
        });

        jPanelResultadoBusqueda.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Búsqueda", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setText("Aproximadamente 1235 resultados (0,15 segundos) ");

        jScrollPane2.setViewportView(jListResultados);

        javax.swing.GroupLayout jPanelResultadoBusquedaLayout = new javax.swing.GroupLayout(jPanelResultadoBusqueda);
        jPanelResultadoBusqueda.setLayout(jPanelResultadoBusquedaLayout);
        jPanelResultadoBusquedaLayout.setHorizontalGroup(
            jPanelResultadoBusquedaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
        );
        jPanelResultadoBusquedaLayout.setVerticalGroup(
            jPanelResultadoBusquedaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelResultadoBusquedaLayout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextFieldSearch)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanelResultadoBusqueda, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonSearch))
                .addGap(4, 4, 4)
                .addComponent(jPanelResultadoBusqueda, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSearchActionPerformed
        //Mostramos los resultados
        if(animator!=null && animator.isRunning()) return;
                isHidden = controls.getHeight()==0;
                animator = new Timer(5, new ActionListener() {
                    @Override public void actionPerformed(ActionEvent e) {
                        controls.revalidate();
                    }
                });
                animator.start();
        //this.jPanelResultadoBusqueda.setVisible(true);
    }//GEN-LAST:event_jButtonSearchActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonSearch;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jListResultados;
    private javax.swing.JPanel jPanelResultadoBusqueda;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextFieldSearch;
    // End of variables declaration//GEN-END:variables
}
